package com.xibei.homepage.controller;

import com.alibaba.fastjson.JSON;
import com.xibei.homepage.UserCourseRequest;
import com.xibei.homepage.UserInfo;
import com.xibei.homepage.service.IUserService;
import com.xibei.homepage.vo.CreateUserRequest;
import com.xibei.homepage.vo.UserCourseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class HomepageUserController {

    private final IUserService userService;

    @Autowired
    public HomepageUserController(IUserService userService) {
        this.userService = userService;
    }
    /**
     * 根据请求对象创建用户信息
     * 本地访问路径：127.0.0.1:7000/homepage-user/create/user
     * 路由访问路径：localhost:9000/imooc/homepage-user/create/user
     * @param request
     * @return
     */
    @PostMapping("/create/user")
    public UserInfo createUser(@RequestBody CreateUserRequest request)
    {
       log.info("<homepage-user>:create user", JSON.toJSONString(request));
       return userService.createUser(request);
    }

    /**
     * 根据id获取用户信息;
     * 请求路径： localhost:9000/imooc/homepage-user/get/user?id=9
     * @param id
     * @return
     */
    @GetMapping("/get/user")
    public UserInfo getUserInfo(Long id)
    {
        log.info("<homepage-user>: get user->{}",id);
        return userService.getUserInfo(id);
    }

    /**
     * 根据id获取用户课程信息
     * 请求路径为： localhost:9000/imooc/homepage-user/get/user/course?id=9
     * 以JSON格式显示;Get方法，
     * @param id
     * @return
     */
    @GetMapping("/get/user/course")
    public UserCourseInfo getUserCourseInfo(Long id)
    {
        log.info("<homepage-user>:get user course info->{}",id);
        return userService.getUserCourseInfo(id);
    }

    /**
     * 设置用户课程信息;
     * 请求路径：localhost:9000/imooc/homepage-user/create/usercourse
     * POS方式，Body: jaw+ JSON(application/json)
     * @param request
     * @return
     */
    @PostMapping("/create/usercourse")
    public UserCourseInfo setUserCourseInfo(@RequestBody UserCourseRequest request)
    {
        return userService.CreateUserCourseInfo(request);
    }
}
